
$(function(){
	//接收参数
	var cases = $("#tabContent > .tab-pane.active").data();
	paramTabPage[tabId] = cases;
	showCondition(false);
	//获取病例编码
	var caseLinkCode=paramTabPage[tabId].caseLinkCode;
	//加载病例
	loadCase(caseLinkCode);
	
	//下一页加载
	$("div[class='pagination'] li a[class='next']").click(function(){
		$("#page").val(parseInt($("#page").val())+1);
	});

	//加载上一页
	$(".previous.paginate_button").click(function(){
		$("#page").val(parseInt($("#page").val())-1);
	});
});
/**
 * 
 * @ Discription:加载病例明细
 * @Autor ：liuhongjun
 * @param caseLinkCode
 * @returns
 * any
 * case.js
 * 2017年6月18日下午6:53:17
 */
function loadCase(caseLinkCode) {
	var tb_id='tb'+new Date().getTime();
	$('#'+tabId+' #content').html('<table class="table display nowrap" style="width:100%;"  id="'+tb_id+'"'
		 		+'style="text-align: center;">'     //transition:all .2s;
		 		+'</table>');
		loadPageData(tabId,tb_id,caseLinkCode);
}
function loadPageData(tabId,tb_id,caseLinkCode,type2){
	var compareType = "比较值";
	switch(paramTabPage[tabId].compareType)
	{
	case "std":
		compareType = "标杆";
		break;
	case "manage":
		compareType = "绩效";
		break;
	case "hosp":
		compareType = "院内均值";
		break;
	}
	var colunms=[ 
	             { 
	 				"data": "MR_NO",title:"病案号",
	 				"render": function(obj, type, full){
	 					var mrNo = full.MR_NO;
	 					return "<a href=\"javascript:medicalRecord('"+full.PMAININDEX+"','"+full.PATIENT_NAME+"')\">"+mrNo+"</a>";
	 				}
	             },
	             { "data": "IP_COUNT",title:"住院次数" },
	             { "data": "PATIENT_NAME",title:"姓名" },
	             { 
	           	  "data": "SEX",title:"性别",
	           	  "render": function(obj, type, full) {
	           		  var a='1 ';
	 		               var sex=$.trim(full.SEX);
	 		               if(sex == "1"){
	 		              	 sex = "男";
	 		               }else if(sex == "2"){
	 		              	 sex = "女";
	 		               }else{
	 		              	 sex = "未知";
	 		               }
	 		               return sex;
	           	  }
	             },
	             { 
	           	  "data": "AGE_YEAR" ,title:"年龄",
	           	  "render": function(obj, type, full) {
	           		  if(full.AGE_YEAR>0){
	           			  return full.AGE_YEAR;
	           		  } else if(full.AGE_MONTH>0 && full.AGE_DAYS>0){
	           			return full.AGE_MONTH + '月' + full.AGE_DAYS + '天';
	           		  } else if(full.AGE_MONTH>0){
	           			  return full.AGE_MONTH + '月';
	           		  } else if(full.AGE_DAYS>0){
	           			  return full.AGE_DAYS + '天';
	           		  } 
	           	  }
	             },
	             { "data": "IN_DATE",title:"入院时间"},
	             { "data": "OUT_DATE",title:"出院时间",
	           	  "render": function(obj, type, full) {
	           		  return full.OUT_DATE;
	           	  }
	             },
	             { "data": "DEP_NAME",title:"出院科室" },
	             {"data":"WARD",title:"出院病房"},
	             { "data": "IN_DAYS",title:"住院日" },
	             { "data": "COSTAMOUNT",title:"总费用(元)", "type":"numeric-comma",
	 				 "render": function(obj, type, full) {
	 					 if(full.MAVGFEE>0 && full.costamount>1*full.MAVGFEE)//超过标杆值
	 		               return "<font color='red'>"+formatNumber(full.COSTAMOUNT)+"</font>";
	 					 else
	 						return formatNumber(full.COSTAMOUNT);
	 		           }
	 			 },
	             { "data": "DRUG_COST" ,title:"药品费用(元)", "type":"numeric-comma",
	 				 "render": function(obj, type, full) {
	 						return formatNumber(full.DRUG_COST);
	 					 }
	             },
	             { "data": "DIAGNOSE_DESC_MAIN",title:"主要诊断","render": function(obj, type, full) {
	            	 return linefeedText(obj,20);
	             }
                 },
                 { "data": "OTHERDIAG",title:"其他诊断","render": function(obj, type, full) {
	            	 return linefeedText(obj);
	             }
                 },
	             { "data": "OPERS" ,title:"手术操作","render": function(obj, type, full) {
	            	 return linefeedText(obj);
	             }
                 },
	             { "data": "PARDRG_CODE",title:"DRG代码"}, 
	             { "data": "DRG_DESC" ,title:"DRG描述","render": function(obj, type, full) {
	            	 return linefeedText(obj);
	             }
	             },
	             { "data": "AS_WEIGHT" ,title:"病组权重"},
	             { "data": "AS_AVG_DAYS" ,title:"平均住院日(标杆值)"},
	             { "data": "AS_AVG_FEE" ,title:"次均费用(元)(标杆值)", "type":"numeric-comma",
	 				 "render": function(obj, type, full) {
	 						return formatNumber(full.AS_AVG_FEE);
	 				 }
	             },
	             { "data": "DIRECTOR",title:"科主任" },
	             { "data": "PROTOMEDICUS",title:"主任（副主任）医师" },
	             { "data": "PHYSICIAN",title:"主治医师" },
	             { "data": "RESIDENT",title:"住院医师" }
	             /*,
	             { "data": "COMPAREGDAYS",title:"平均住院日("+compareType+")" },
	             { "data": "COMPAREGFEE" ,title:"次均费用(元)("+compareType+")", "type":"numeric-comma",
	 				 "render": function(obj, type, full) {
	 						return formatNumber(full.COMPAREGFEE);
	 				 }
	             }*/
	             ];
	var ajaxFunction=function (aoData, fnCallback,oSetting ) {
		orderColumn=colunms[aoData.order[0].column].data;//获取排序的字段
		orderMethod=aoData.order[0].dir;//获取asc desc
     	 var iDisplayStart=aoData.start;
     	 var iDisplayLength=aoData.length;
     	 var sEcho = 1;
     	 
     	 //获取病例总条数
     	 var caseTotalCount = parseInt($('#'+tabId+' #caseTotalCount').val());
     	 var param=drgUI.setAjaxData(tabId);
     	 param.quotaCode=caseLinkCode;
     	 param.type2=type2;
      	 param.page=iDisplayStart; //开始行数
     	 param.pageSize=iDisplayLength;
     	 param.sEcho=sEcho;
     	 param.caseTotalCount=caseTotalCount;
     	 param.orderColumn=orderColumn;
     	 param.orderMethod=orderMethod;
     	 $.ajax( {
	             "dataType": 'json', 
	             "type": "POST", 
	             "url": contextPath + "/DrgsIndexCal/findCaseList.dhc",
	             "data": param,
	             "success": function(resp) {
	            	var data=[];
	            	 if(resp.success){
	            		$('#'+tabId+' #caseTotalCount').val(resp.data.data.iTotalDisplayRecords);//总条数赋值到隐藏域
	            	 }
	            	 data.draw=aoData.draw;
	            	 data.recordsTotal=resp.data.data.iTotalRecords;
	            	 data.recordsFiltered=resp.data.data.iTotalDisplayRecords;
	            	 data.data=resp.data.data.aaData;
					fnCallback(data,oSetting);
	             }
        } );
      
     };
     // 大于60天病例数 和 未入组病例数 的病例明细隐藏关于drg的信息 { "visible": false, "targets": [2,3,6,8,10,11,20]}
     var columnDefs = [{"orderable": true,"targets": [5,6,9,10,16] },{"orderable": false,"targets": '_all' }] ;
     if(caseLinkCode == "NotInCaseCnt" || caseLinkCode == "Gt60") {
    	 columnDefs.push({ "visible": false, "targets": [13,14,15,16,17,18,19] }) ;
     }
     var paramsDataTables={ajaxFunction:ajaxFunction,columnDefs:columnDefs,tableTitle:'病例明细表',scrollX:true,scrollY:'350px',autoWidth:false,orderCellsTop:true,pageLength:50,lengthMenu:[[ 50,100,500, 1000,99999],[ 50,100,500, 1000,'全部']]};
    var oTable=$('#'+tabId+' #'+tb_id).dataTableServerPaging(colunms,paramsDataTables);
    $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollBody table tbody").attr('id','table');//tbody加上ID，病案号的样式
    $('#'+tabId+' #'+tb_id+"_wrapper .dataTables_scrollHeadInner table thead th").css({'text-align':'center'});
	oTable.columns.adjust();//tbody加了样式，重新调整宽度
	if(type2){
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室、DRG代码' placeholder='病案号、姓名、出院科室、DRGs代码'  value='"+type2+"'></label>")
	}else{
		$("#"+tabId+" #"+tb_id+"_filter").html("<label>搜索 <input type='text' id='"+tb_id+"_filter_value' title='病案号、姓名、出院科室、DRG代码' placeholder='病案号、姓名、出院科室、DRGs代码'></label>")
	}
	//重写过滤方法
	$("#"+tabId+" #"+tb_id+"_filter_value").keydown(function(e){
		if(e.keyCode==13){
			var type2 = $("#"+tabId+" #"+tb_id+"_filter_value").val();
			if(type2.length==0){
				$('#'+tabId+' #caseTotalCount').val(0);//总条数赋值到隐藏域
			}
			loadPageData(tabId,tb_id,caseLinkCode,type2);
		}
	});
}

